package com.woniu.finance.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.finance.entity.*;
import com.woniu.finance.mapper.*;
import com.woniu.finance.service.IFinanceService;
import com.woniu.util.utils.HttpResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 融资企业表 服务实现类
 * </p>
 *
 * @author 刘品水
 * @since 2023-04-25
 */
@Service
public class FinanceServiceImpl  implements IFinanceService {
    @Autowired(required = false)
    private FinanceMapper financeMapper;
    @Autowired(required = false)
    private UserMapper userMapper;
    @Autowired(required = false)
    private UserRoleMapper userRoleMapper;
    @Override
    public HttpResult<?> findAllFinance() {
        try {
            List<Finance> finances = financeMapper.selectList(null);
            return new HttpResult<>().ok(finances);
        } catch (Exception e) {
            e.printStackTrace();
            return new HttpResult<>().fail();
        }
    }

    @Override
    @Transactional
    public HttpResult<?> saveFinance(Finance finance) {
        StringBuffer k = new StringBuffer();

        for(int i=0; i< 6; i++) {
            k.append((int)(Math.random() * 10));
        }
        String password = k.toString();
        try {
            QueryWrapper<Finance> wrapper = new QueryWrapper<>();
            wrapper.eq("enterprise_name",finance.getEnterpriseName());
            wrapper.eq("legal",finance.getLegal());
            Finance finance1 = financeMapper.selectOne(wrapper);
            if(finance1!=null){
                return new HttpResult().fail();
            }else{
                financeMapper.insert(finance);//新增企业

                //生成账号
                FinanceUser financeUser = new FinanceUser();
                financeUser.setUserName(finance.getLegalPhone());
                financeUser.setEnterpriseId(finance1.getEnterpriseId());
                financeUser.setPasswd(password);
                userMapper.insert(financeUser);
                //根据账号查找生成的账号信息
                QueryWrapper<FinanceUser> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("user_name",finance.getLegalPhone());
                FinanceUser financeUser1 = userMapper.selectOne(queryWrapper);
                //给账号赋角色
                UserRole userRole = new UserRole();
                userRole.setUserId(financeUser1.getId());
                userRole.setRoleId(1);
                userRoleMapper.insert(userRole);
                return new HttpResult<>().ok();
            }

        } catch (Exception e) {
           throw e;
        }

    }

    @Override
    public HttpResult<?> modifyFinance(Finance finance) {
        try {
            financeMapper.updateById(finance);
            return new HttpResult<>().ok();
        } catch (Exception e) {
            e.printStackTrace();
            return new HttpResult<>().fail();
        }
    }

    @Override
    public HttpResult<?> removeFinanceById(String enterpriseId) {
        try {
            financeMapper.deleteById(enterpriseId);
            return new HttpResult<>().ok();
        } catch (Exception e) {
            e.printStackTrace();
            return new HttpResult<>().fail();
        }
    }

    @Override
    public HttpResult<?> findFinanceById(String enterpriseId) {
        try {
            Finance finance = financeMapper.selectById(enterpriseId);
            return new HttpResult<>().ok(finance);
        } catch (Exception e) {
            e.printStackTrace();
            return new HttpResult<>().fail();
        }
    }

    @Override
    public HttpResult<?> queryFinanceInfoByPage(RequestParam<Finance> param) {
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<Finance> financeInfo = financeMapper.queryFinanceByEnterpriseName(param.getData().getEnterpriseName(),param.getData().getCoreState(),param.getData().getPlatformState());
        PageInfo pageInfo = new PageInfo(financeInfo);
        return new HttpResult<>().ok(pageInfo);
    }

    @Override
    public HttpResult queryFinanceIntoPlatformRecommendationRecord(RequestParam<Finance> param) {
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<Finance> list = financeMapper.queryFinanceIntoPlatformRecommendationRecord(param.getData().getEnterpriseName());
        PageInfo pageInfo = new PageInfo(list);
        return new HttpResult<>().ok(pageInfo);
    }




    /**
     * 根据企业名称查询企业信息
     * @param enterpriseName 企业名称
     * @return
     */
    public Finance findFinanceByName(String enterpriseName){
        try {
            QueryWrapper<Finance> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("enterprise_name",enterpriseName);
            Finance finance = financeMapper.selectOne(queryWrapper);
            if(finance != null){
                return finance;
            }
            return null;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 根据企业名称模糊查询企业信息
     * @param param
     * @return
     */
    public HttpResult<?> findListFinanceByName(RequestParam<Finance> param){
        try {
            PageHelper.startPage(param.getPageNum(), param.getPageSize());
            List<Finance> financeList = financeMapper.findListFinanceByName(param.getData().getEnterpriseName(),param.getData().getEnterpriseId());
            PageInfo pageInfo = new PageInfo<>(financeList);
            if(financeList != null && financeList.size() > 0){
                return new HttpResult<>().ok(pageInfo);
            }
            return new HttpResult<>().fail();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    /**
     * 根据id集合 分页 查询融资企业信息
     * @param param 融资企业id集合
     * @return
     */
    @Override
    public HttpResult<?> findFinanceByListId(RequestParam<List> param) {
        try {
            PageHelper.startPage(param.getPageNum(), param.getPageSize());
            List<Finance> financeList = financeMapper.findFinanceByListId(param.getData());
            PageInfo pageInfo = new PageInfo<>(financeList);
            if(financeList != null && financeList.size() > 0){
                return new HttpResult<>().ok(pageInfo);
            }
            return new HttpResult<>().fail();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


}
